﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinForm {
    public partial class FormContactAdd : Form
    {
        BLL.Contact contact = new BLL.Contact();
        public FormContactAdd()
        {
            InitializeComponent();
        }

        private void FormContactAdd_Load(object sender, EventArgs e)
        {
            FillGroup();
        }
        
        public void FillGroup()
        {
            BLL.ContactGroup groupBLL = new BLL.ContactGroup();
            
            cboGroup.DisplayMember = "groupname";
            cboGroup.ValueMember = "id";
            cboGroup.DataSource = groupBLL.GetList("");
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(cboGroup.SelectedValue.ToString());
            Model.Contact model = new Model.Contact();
            model.Name = txtName.Text.Trim();
            model.Phone = txtPhone.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            model.QQ = txtQQ.Text.Trim();
            model.OfficeAddress = txtWorkAddress.Text.Trim();
            model.OfficePhone = txtWorkPhone.Text.Trim();
            model.HomeAddress = txtHomeAddress.Text.Trim();
            model.HomePhone = txtHomePhone.Text.Trim();
            model.Memo = txtMemo.Text.Trim();
            //获取用户选择的分组编号
            model.GroupId = Convert.ToInt32(cboGroup.SelectedValue);
            string msg = "";
            if (contact.Add(model,out msg))
            {
                MessageBox.Show("添加联系人成功。");
                this.Close();
            }
            else
            {
                MessageBox.Show("添加联系人失败。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
